package com.fps.webshop.question.question0001_0099;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 36. 有效的数独
 * 
 * 请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ，验证已经填入的数字是否有效即可。
 * 
 * 数字 1-9 在每一行只能出现一次。
 * 数字 1-9 在每一列只能出现一次。
 * 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。（请参考示例图）
 * 数独部分空格内已填入了数字，空白格用 '.' 表示。
 * 
 * 注意：
 * 		一个有效的数独（部分已被填充）不一定是可解的。
 * 		只需要根据以上规则，验证已经填入的数字是否有效即可。
 * 
 * 示例 1：
 * Question036_01.png
 * 
 * 输入：board = 
 * [["5","3",".",".","7",".",".",".","."]
 * ,["6",".",".","1","9","5",".",".","."]
 * ,[".","9","8",".",".",".",".","6","."]
 * ,["8",".",".",".","6",".",".",".","3"]
 * ,["4",".",".","8",".","3",".",".","1"]
 * ,["7",".",".",".","2",".",".",".","6"]
 * ,[".","6",".",".",".",".","2","8","."]
 * ,[".",".",".","4","1","9",".",".","5"]
 * ,[".",".",".",".","8",".",".","7","9"]]
 * 输出：true
 * 
 * 示例 2：
 * 输入：board = 
 * [["8","3",".",".","7",".",".",".","."]
 * ,["6",".",".","1","9","5",".",".","."]
 * ,[".","9","8",".",".",".",".","6","."]
 * ,["8",".",".",".","6",".",".",".","3"]
 * ,["4",".",".","8",".","3",".",".","1"]
 * ,["7",".",".",".","2",".",".",".","6"]
 * ,[".","6",".",".",".",".","2","8","."]
 * ,[".",".",".","4","1","9",".",".","5"]
 * ,[".",".",".",".","8",".",".","7","9"]]
 * 输出：false
 * 解释：除了第一行的第一个数字从 5 改为 8 以外，空格内其他数字均与 示例1 相同。 
 * 	   但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
 * 提示：
 * 		board.length == 9
 * 		board[i].length == 9
 * 		board[i][j] 是一位数字或者 '.'
 */
public class Question036 {

	/**
	 * 第一次提交，使用Map记录每个元素的位置，然后进行判断
	 * 
	 * 提交结果	执行用时	内存消耗	语言		提交时间				备注 
	 * 通过 		2 ms	38.4 MB	Java	2021/08/16 10:35
	 * 
	 * 执行用时：2 ms, 在所有 Java 提交中击败了77.31%的用户
	 * 内存消耗：38.6 MB, 在所有 Java 提交中击败了40.57%的用户
	 */
	public static boolean isValidSudoku1(char[][] board) {
		Map<Character,List<int[]>> map = new HashMap<Character,List<int[]>>();
		for(int i=0;i<board.length;i++) {
			for(int j=0;j<board[i].length;j++) {
				if('.'==board[i][j]) {
					continue;
				}
				List<int[]> list = map.get(board[i][j]);
				if(list==null) {
					list = new ArrayList<int[]>();
				}else {
					for(int[] other:list) {
						if(other[0]==i||other[1]==j) {
							return false;
						}
						if(other[0]/3==i/3&&other[1]/3==j/3) {
							return false;
						}
					}
				}
				list.add(new int[] {i,j});
				map.put(board[i][j],list);
			}
		}
		return true;
    }
	/**
	 * 第二次提交，不使用Map记录每个元素的位置，而是分别用三个数组分别记录行、列、第几个九宫格这三个维度去进行记录
	 * 但空间消耗很大
	 * 
	 * 提交结果	执行用时	内存消耗	语言		提交时间				备注 
	 * 通过 		1 ms	38.6 MB	Java	2021/08/16 10:42
	 * 
	 * 执执行用时：1 ms, 在所有 Java 提交中击败了100.00%的用户
	 * 内存消耗：38.6 MB, 在所有 Java 提交中击败了29.55%的用户
	 */
	public boolean isValidSudoku2(char[][] board) {
		int length = board.length;
		int[][] row = new int[length][length];
		int[][] column = new int[length][length];
		int[][] col = new int[length][length];
		for (int i = 0; i < length; i++) {
			for (int j = 0; j < length; j++) {
				if (board[i][j] == '.') {
					continue;
				}
				int num = board[i][j] - '0' -1;
				int k = i/3 * 3 + j / 3;
				if (row[i][num]!=0 || column[j][num]!=0 || col[k][num]!=0) {
					return false;
				}
				row[i][num] = column[j][num] = col[k][num] = 1;
			}
		}
		return true;
	}
	public static void main(String[] args) {
		char[][] board = new char[][] {
			{'.','.','.','.','5','.','.','1','.'},
			{'.','4','.','3','.','.','.','.','.'},
			{'.','.','.','.','.','3','.','.','1'},
			{'.','.','.','.','.','.','.','.','.'},
			{'.','.','.','.','.','.','.','.','.'},
			{'.','.','.','.','.','.','.','.','.'},
			{'.','.','.','.','.','.','.','.','.'},
			{'.','.','.','.','.','.','.','.','.'},
			{'.','.','.','.','.','.','.','.','.'}
		};	
		System.out.print(isValidSudoku1(board));
	}
}
